import requestUtil from "@/utils/requestUtil";

const MATERIAL_BASE_URL = "/business/material";
import type { UploadUserFile } from "element-plus";
import { Attachment, IObject } from "@/api/common/common";

const MaterialAPI = {
  /**
   * 获取材料分页数据
   */
  getPage(queryParams?: MaterialPageQuery) {
    return requestUtil.get<PageResult<MaterialPageVO[]>>(
      `${MATERIAL_BASE_URL}/loadPage`,
      queryParams
    );
  },

  /**
   * 根据类型 获取材料备选
   */
  loadOptions(type?: string) {
    return requestUtil.get<OptionType[]>(`${MATERIAL_BASE_URL}/loadOptions?type=${type}`);
  },

  /**
   * 获取材料表单数据
   * @param id 材料ID
   * @returns 材料表单数据
   */
  getFormData(id: string) {
    return requestUtil.get<MaterialForm>(`${MATERIAL_BASE_URL}/load/${id}`);
  },

  /** 添加材料 */
  create(data: MaterialForm) {
    return requestUtil.postFileForm<MaterialForm>(`${MATERIAL_BASE_URL}/add`, data);
  },

  /**
   * 更新材料
   * @param id 材料ID
   * @param data 材料表单数据
   */
  update(data: MaterialForm) {
    return requestUtil.postFileForm<MaterialForm>(`${MATERIAL_BASE_URL}/update`, data);
  },

  /**
   * 批量删除材料，多个以英文逗号(,)分割
   * @param ids 材料ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids: string) {
    return requestUtil.get<number>(`${MATERIAL_BASE_URL}/batchDelete?ids=${ids}`);
  },

  deleteById(id: string) {
    return requestUtil.get<number>(`${MATERIAL_BASE_URL}/delete/${id}`);
  },

  /**
   * 导入套餐/材料到商品
   * @param categoryId
   * @param ids
   * @returns
   */
  transToGoods(categoryId: string, ids: string[]) {
    let data = { categoryId, materialIds: ids };
    return requestUtil.post<number>(`${MATERIAL_BASE_URL}/transToGoods`, data);
  },
};

export default MaterialAPI;

/** 分页查询参数 */
export interface MaterialPageQuery extends PageQuery {
  /** 搜索关键字 */
  keywords?: string;
  type?: MaterialType;
  related?: boolean;
  categoryIds?: string;
  deepSearch?: boolean;
}

/** 分页对象 */
export interface MaterialPageVO {
  /** 材料ID */
  id?: string;
  /** 材料名称 */
  name?: string;
  /** 编码 */
  code?: string;
  /** 材料 */
  price?: string;
  unit?: string;
  type?: MaterialType;
  remark?: string;
  /**成本价 */
  costPrice?: string;
  spec?: string;
}

/** 分页对象 */
export interface MaterialChooseVO extends MaterialPageVO {
  num?: number;
}

/** 材料表单对象 */
export interface MaterialForm {
  /** 材料ID */
  id?: string;
  /** 材料名称 */
  name?: string;
  /** 编码 */
  code?: string;
  /** 材料 */
  price?: number | string;
  unit?: string;
  type?: MaterialType;
  remark?: string;
  files?: UploadUserFile[];
  attachments?: Attachment[];
  keepIds?: number[];
  /**成本价 */
  costPrice?: number | string;
  spec?: string;
  categoryId?: string;
}

export enum MaterialType {
  MAIN = "MAIN",
  SUB = "SUB",
  INSTRUMENT = "INSTRUMENT",
  OTHER = "OTHER",
}

export enum MaterialAllType {
  MAIN = "MAIN",
  SUB = "SUB",
  INSTRUMENT = "INSTRUMENT",
  OTHER = "OTHER",
  NORM = "NORM",
}

export const Material_LABEL: IObject = {
  [MaterialType.MAIN]: "主材",
  [MaterialType.SUB]: "辅材",
  [MaterialType.INSTRUMENT]: "器具",
  [MaterialType.OTHER]: "其他",
  [MaterialAllType.NORM]: "定额",
};
